package com.gamecauca.facebook;





import java.io.IOException;
import java.net.MalformedURLException;


import com.gamecauca.common.CustomProgressDialog;
import com.gamecauca.facebook.Facebook.DialogListener;
import com.gamecauca.facebook.SessionEvents.AuthListener;
import com.gamecauca.facebook.SessionEvents.LogoutListener;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ApiShareLink {

	private Activity activity;
	private Facebook mFacebook = null;
	
	private String APP_ID = "111267142343639"; // APP_ID for user contact with facebook
	private boolean checkLogin = false;
	private Bundle params = null;
	private String[] mpermision = new String[] {"publish_stream"};
	private Handler fbHandler;
	
	
	public ApiShareLink(Activity activity, Handler fbHandler){
		
		this.activity = activity;
		this.fbHandler = fbHandler;
		
		mFacebook = new Facebook(APP_ID);
		mFacebook.setAccessExpires(1000000);
       	SessionStore.restore(mFacebook, this.activity);
       	SessionEvents.addAuthListener(new SessionListener());
       	
       	params = new Bundle();			
		params.putString("method", "links.post");
		params.putString("access_token", mFacebook.getAccessToken());
	}
	
	public void setUrl(String url) {
		params.putString("url", url);
	}
	
	public void setComment(String comment) {
		params.putString("comment", comment);
	}
	
	public boolean checkLogin() {
		if(mFacebook.isSessionValid()){
			return true;
		}
		return false;
	}
	
	private void msgFbStatus(boolean status){
		
		Message myMessage = new Message();
	    Bundle resBundle = new Bundle();
	    
	    resBundle.putBoolean("fbStatus", status);
	    
	    myMessage.setData(resBundle);
	    fbHandler.sendMessage(myMessage);
	}
	
	public boolean share(CustomProgressDialog progressDialog) {
		if(checkLogin()){
			try {
				String res = mFacebook.request(params);
				msgFbStatus(true);
//		        Log.i("DUOC-ApiShareLink", "Share" + res);
		       
			} catch (MalformedURLException e) {
//				e.printStackTrace();
				msgFbStatus(false);
			} catch (IOException e) {
//				e.printStackTrace();
				msgFbStatus(false);
			}
		}
		else {
			
			mFacebook.authorize(this.activity, mpermision, new LoginDialogListener());
			progressDialog.callBack().cancel();
//	        Log.i("DUOC-ApiShareLink", "Login agian");
		}
		
		return true;
	}
	
	private final class LoginDialogListener implements DialogListener {
			
        public void onComplete(Bundle values) {
            SessionEvents.onLoginSuccess();
//            Log.i("DUOC-ApiShareLink", "On Complete");         
        }

        public void onFacebookError(FacebookError error) {
            SessionEvents.onLoginError(error.getMessage());
//            Log.i("DUOC-ApiShareLink", "On Facebook Error");
           
        }
        
        public void onError(DialogError error) {
            SessionEvents.onLoginError(error.getMessage());
//            Log.i("DUOC-ApiShareLink", "On Error");
          
        }

        public void onCancel() {
            SessionEvents.onLoginError("Action Canceled");
//            Log.i("DUOC-ApiShareLink", "On Cancel");
            
        }
    }
	
	private class SessionListener implements AuthListener, LogoutListener {

		@Override
		public void onLogoutBegin() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onLogoutFinish() {
			// TODO Auto-generated method stub
			SessionStore.clear(activity.getApplicationContext());
		}

		@Override
		public void onAuthFail(String error) {
			// TODO Auto-generated method stub
			checkLogin = false;
		}

		@Override
		public void onAuthSucceed() {
			// TODO Auto-generated method stub
			SessionStore.save(mFacebook, activity.getApplicationContext());
			checkLogin = true;
			
			try {
				String res = mFacebook.request(params);
//				Log.e("DUOC", "RESQUEST : " + res);
				
				
			}catch (MalformedURLException e) {
				Log.e("ApiShareLink", "=> " + e);
				msgFbStatus(false);
				
			} catch (IOException e) {
				Log.e("ApiShareLink", "=> " + e);
				msgFbStatus(false);
				
			}catch(Exception e){
				Log.e("ApiShareLink", "=> " + e);
				msgFbStatus(false);
			}
			
		}
		
	}
	
	
}
